﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Configuration;
using SFrost;

namespace ReportMobile {
    public partial class InBU : System.Web.UI.Page {
        public SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnName"].ToString());
        public SqlCommand cmd = new SqlCommand();
        public DataSet ds = new DataSet();
        public DataTable dt;
        public DataRow dr;
        public DataSet dsDetail = new DataSet();
        public DataTable dtDetail;
        public DataRow drDetail;
        public string uId = "";
        public string msg = "";
        public DateTime rptDate;
        public StringBuilder sbVariety = new StringBuilder();  //正式，品类
        protected void Page_Load(object sender, EventArgs e) {
            rptDate = DateTime.Now;
            if (Request.QueryString["uid"] == null || Request.QueryString["uid"].Trim() == "") {
                uId = "";
                return;
            }
            else {
                uId = Request.QueryString["uid"].ToString();
            }
            Frost f = new Frost();
            cmd.Connection = conn;
            conn.Open();
            StringBuilder sb = new StringBuilder("select FId, FOAId, FVariety, FRegDate,funitid,funitcustom from TAuth where foaid = '" + uId + "'");
            if (f.selectCount(sb) < 1) {
                msg = "未查找到用户：" + uId + " 的授权，请与系统管理员联系。";
                return;
            }
            dt = f.getDataTable(sb);
            dr = dt.Rows[0];
            
            string[] arrayUnitID;
            if (dr["funitid"] != null) {
                //开始取数前，再确认一次是否有查看事业部数据的权限
                //手机端导航条可能未退出，但后台数据发生变化
                if(dr["funitid"].ToString().IndexOf('1') < 0) {
                    msg = "查看【事业部】数据权限，请联系管理员。";
                    return;
                }
                arrayUnitID = dr["funitid"].ToString().Split(',');
            }
            //加工成sql where品类
            string[] authVariety = dr["fvariety"].ToString().Split(',');
            if (dr["fvariety"] !=null){
                for (int i = 0; i < authVariety.Length; i++)
                {
                    sbVariety.Append(",'" + authVariety[i].Trim().Replace(" ", "") + "'");
                }
                sbVariety.Remove(0, 1);
            }
            sb.Clear();
            //事业部不存在公司限制，所以不用设置查询公司范围
            sb.Append("select 品类,SUM(欠款余额) 欠款余额 from View_OA_客户借贷款");
            sb.AppendFormat(" where 品类 in ({0})", sbVariety.ToString());
            sb.Append(" and 欠款余额 >= 0");
            sb.Append(" group by 品类");
            sb.Append(" order by 品类 desc");
            cmd.CommandText = sb.ToString();
            try {
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                sda.Fill(ds);
                dt = ds.Tables[0];
                cmd.Dispose();
                sb.Clear();
                sb.Append("select 品类,借款类型,SUM(欠款余额) 欠款余额 from View_OA_客户借贷款");
                sb.AppendFormat(" where 品类 in ({0})", sbVariety.ToString());
                sb.Append(" and 欠款余额 >= 0");
                sb.Append(" group by 品类,借款类型");
                cmd.CommandText = sb.ToString();
                sda.Fill(dsDetail);
                dtDetail = dsDetail.Tables[0];
            }
            catch (Exception er) {
                msg = er.Message;
            }
            f.close();
        }
    }
}